import Vue from 'vue'
import Vuex from 'vuex'
import reshandler from 'services/response-handler'
/* 订单列表（新接口） */
// 订单列表（新接口）response
function getListOrderExResponse(response) {
  return Object.assign({}, response, {
    orders: response.orders || []
  })
}
// 订单列表（新接口）
export function ListOrderEx(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "ListOrderEx",
  })

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getListOrderExResponse), reshandler.error(fail))
}

/* 更新订单 */
// 更新订单 response
function getUpdateOrderResponse(response) {
  return Object.assign({}, response, {})
}
// 更新订单
export function UpdateOrder(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "UpdateOrder",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getUpdateOrderResponse), reshandler.error(fail))
}

/* 支付 */
// 支付 response
function getBalancePaymentResponse(response) {
  return Object.assign({}, response, {})
}
// 支付
export function BalancePayment(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "BalancePayment",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getBalancePaymentResponse), reshandler.error(fail))
}

/* 重新购买, 添加商品到购物车AddCartItem */
// 添加商品到购物车
export function AddCartItem(requests, success, fail) {
  // fail init
  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }
  // request
  let promises = [];
  requests.forEach(req => {
    promises.push(this.$.get(Object.assign({}, req, {
      methodName: "AddCartItem",
    })))
  })

  promises.length > 0 && Promise.all(promises).then(success, fail)
}

/* 查询订单详情 */
function getQueryOrderDetailResponse(response) {
  return Object.assign({}, response, {})
}
// 订单详情
export function QueryOrderDetail(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "QueryOrderDetail",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getQueryOrderDetailResponse), reshandler.error(fail))
}

/* 取消申请 */
function getDeleteRefundsResponse(response) {
  return Object.assign({}, response, {})
}
// 订单详情
export function DeleteRefunds(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "DeleteRefunds",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getDeleteRefundsResponse), reshandler.error(fail))
}


// Vuex
// const addressStore = new Vuex.Store({
//   state:{
//     address: null
//   },
//   getters:{
//     getAddress:function (state) {
//       if(!state.address){
//         state.address = null
//       }
//       return state.address;
//     }
//   },
//   mutations:{
//     setAddress(state, address) {
//       state.address = address;
//     }
//   }
// })

// export default addressStore